'use strict'

###*
 # @ngdoc function
 # @name xoceanApp.controller:TemplatesCtrl
 # @description
 # # TemplatesCtrl
 # Controller of the page named '我的专题'
###
angular.module('xoceanApp')
  .controller 'TemplatesCtrl', ($scope, $filter, Template, templates, ngDialog) ->
    currentTab = 'hot'
    templates = $filter('orderBy') templates, 'count', true
    _templates = []
    userid = ''
    Template.getUserId().then (id)->
      userid = id
      return

    $scope.pageChanged = ->
      $scope.templates = _templates.slice ($scope.currentPage - 1) * 8 , $scope.currentPage * 8 
      return

    $scope.onclickTab = (clickedTab)->
      currentTab = clickedTab
      if clickedTab is 'hot'
        _templates = $filter('orderBy') templates, 'count', true
      else if clickedTab is 'new'
        _templates = $filter('orderBy') templates, 'ptime', true
      else
        _templates = $filter('filter') templates, {'userid': userid}
      
      $scope.currentPage = 1
      $scope.totalItems = _templates.length
      $scope.templates = _templates.slice(0,8)

      return

    $scope.isActive = (tab)->
      return tab is currentTab

    $scope.onclickTab(currentTab)

    _index = 0

    $scope.tmplDetail = (index)->
      _index = index
      $scope.tmpl = $scope.templates[index]
      ngDialog.open({
        template: 'dialog4'
        scope: $scope
        className: 'ngdialog-template-preview'
      })
      return

    $scope.prev = ()->
      return if _index is 0
      _index--
      $scope.tmpl = $scope.templates[_index]
      return

    $scope.next = ()->
      return if _index is $scope.templates.length - 1
      _index++
      $scope.tmpl = $scope.templates[_index]
      return

    return
    


